﻿using System;
using System.Diagnostics;
using BrnMall.Core;
using BrnMall.Services;
using WeiTap.Restful;

namespace QIDong.WeApp.Controllers
{
    public class CouponController : WebApi
    {
        private static object _locker = new object();//锁对象
        /// <summary>
        /// 领取优惠劵
        /// </summary>
        public string GetCoupon(int couponTypeId,int uid)
        {
            try
            {
                lock (_locker)
                {

                    CouponTypeInfo couponTypeInfo = Coupons.GetCouponTypeById(couponTypeId);
                    //判断优惠劵类型是否存在
                    if (couponTypeInfo == null || couponTypeInfo.SendMode != 0)
                        throw new Exception("优惠劵不存在");
                    //判断优惠劵类型是否开始领取
                    if (couponTypeInfo.SendStartTime > DateTime.Now)
                        throw new Exception("该优惠劵还未开始");
                    //判断优惠劵类型是否结束领取
                    if (couponTypeInfo.SendEndTime <= DateTime.Now)
                        throw new Exception("该优惠劵已过期");
                    //判断优惠劵类型是否已经领取
                    if (couponTypeInfo.GetMode == 1 && Coupons.GetSendUserCouponCount(uid, couponTypeId) >= 1)
                        throw new Exception("该优惠劵限领一张");
                    if(couponTypeInfo.GetMode == 2 && Coupons.GetTodaySendUserCouponCount(uid, couponTypeId, DateTime.Now) >= 1)
                        throw new Exception("该优惠劵每天限领一张");
                    //判断优惠劵是否已经领尽
                    int sendCount = Coupons.GetSendCouponCount(couponTypeId);
                    if (sendCount >= couponTypeInfo.Count)
                        throw new Exception("该优惠劵已领尽");
                    var partUserInfo = Users.GetPartUserById(uid);
                    string couponSN = Coupons.PullCoupon(partUserInfo, couponTypeInfo, DateTime.Now, WebHelper.GetIP());
                    return couponSN;
                }
            }
            catch (Exception e)
            {
                Trace.TraceError(e.StackTrace);
                throw;
            }
            
        }
        /// <summary>
        /// 验证优惠劵编号
        /// </summary>
        /// <returns></returns>
        public bool VerifyCouponSn(string couponSn)
        {
            try
            {
                if (couponSn.Length == 0)
                    throw new Exception("请输入优惠劵编号");
                else if (couponSn.Length != 16)
                    throw new Exception("优惠劵编号不正确");

                CouponInfo couponInfo = Coupons.GetCouponByCouponSN(couponSn);
                if (couponInfo == null)//不存在
                    throw new Exception("优惠劵不存在");
                else if (couponInfo.Oid > 0)//已使用
                    throw new Exception("优惠劵已使用");
                else if (couponInfo.Uid > 0)//已激活
                    throw new Exception("优惠劵已激活");
                else//未激活
                    return true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
            
        }
    }
}
